//
//  main.c
//  链式表
//
//  Created by 解楚豪 on 2018/8/8.
//  Copyright © 2018年 解楚豪. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

typedef struct Link{
    char data;//数据
    struct Link *next;//下一个区域指针
}link;

typedef struct com{
    int data;//数据
    int cur;//游标
}component;
//初始化
link *initLink(){
    link *lk = (link *)malloc(sizeof(link));
    link *temp = lk;
    //生产
    for (int i=1; i<5; i++) {
        link *a = (link *)malloc(sizeof(link));
        a -> data = i;
        a -> next = NULL;
        temp -> next = a;
        temp = temp -> next;
    }
    return lk;
}
//增加
link *addLink(link *lk,int data,int add){
    link *temp = lk;
    for (int i=1; i<add; i++) {
        if (temp == NULL) {
            printf("报错");
            return temp;
        }
        temp = temp -> next;
    }
    
    link *a = (link *)malloc(sizeof(link));
    a -> data = data;
    a -> next = temp ->next;
    temp -> next = a;
    return lk;
}
//删除
link *delLink(link *lk, int add){
    link *temp = lk;
    for (int i=1; i<add; i++) {
        if (temp == NULL) {
            printf("失败");
            return temp;
        }
        temp = temp -> next;
    }
    
    link *del = temp -> next;
    temp -> next = temp -> next -> next;
    free(del);
    return lk;
}

//改
link *editLink(link *lk,int add,int newData){
    link *temp = lk;
    temp = temp -> next;
    for (int i=0; i<add; i++) {
        temp = temp -> next;
    }
    temp -> data = newData;
    return lk;
}

//查
int selectLink(link *lk ,int data){
    link *t = lk;
    int i = 1;
    while (t -> next) {
        t = t -> next;
        if (t -> data == data) {
            return i;
        }
        i++;
    }
    return -1;
}

//输出
void display(link *lk){
    link *link =lk;
    while (link -> next) {
        link = link -> next;
        printf("%d ", link -> data);
    }
    
    printf("\n");
}


void reserverArr(component *array){
    for (int i=0; i<9; i++) {
        array[i].cur = i+1;
    }
    array[4-1].cur = 0;
}

int mallocArr(component *array){
    int i = array[0].cur;
    if (array[0].cur) {
        array[0].cur = array[i].cur;
    }
    
    return i;
}

void initArr(component *array){
    reserverArr(array);
    //从备用链表中拿出一个分量作为链表的头结点，返回的是这个分量的下标
    int body = mallocArr(array);
    //声明一个变量，把它当指针使，指向链表的最后的一个结点，因为链表为空，所以和头结点重合
    int tempBody = body;
    for (int i=0; i<4; i++) {
        int j=mallocArr(array);//从备用链表中拿出空闲的分量
        array[tempBody].cur=j;//将申请的空线分量链接在链表的最后一个结点后面
        array[j].data='a'+i-1;//给新申请的分量的数据域初始化
        tempBody=j;//将指向链表最后一个结点的指针后移
    }
    array[tempBody].cur=0;//新的链表最后一个结点的指针设置为0
    
}

int main(int argc, const char * argv[]) {
    /*--------------------动态链表---------------------*/
    printf("初始化:\n");
    link *p = initLink();
    display(p);
    
    //曾
    printf("增加:\n");
    p = addLink(p, 10, 3);
    display(p);
    
    //删
    printf("删除\n");
    p = delLink(p, 2);
    display(p);
    
    //改
    printf("修改");
    p = editLink(p, 2, 87);
    display(p);
    //查
    printf("查找");
    int addIndex = selectLink(p, 87);
    if (addIndex == -1) {
        printf("查询失败,没有这个");
    }else{
        printf("%d \n",addIndex);
    }
    
    
    /*---------------------------静态链表------------------------*/
    
    return 0;
}


#pragma makr -------------------静态链表----------------------------------
